package com.prs.crm.action.cdxh;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.cdxh.cd_ProductClass;
import com.prs.crm.service.cdxh.cd_ProductClassService;

public class cd_ProductClassAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private cd_ProductClass cdProductClass;
	private cd_ProductClassService cd_ProductClassService;
	private Integer[] ids;
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public cd_ProductClassService getCd_ProductClassService() {
		return cd_ProductClassService;
	}
	public void setCd_ProductClassService(
			cd_ProductClassService cdProductClassService) {
		cd_ProductClassService = cdProductClassService;
	}

	private PaginationSupport pagination; 
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public cd_ProductClass getCdProductClass() {
		return cdProductClass;
	}
	public void setCdProductClass(cd_ProductClass cdProductClass) {
		this.cdProductClass = cdProductClass;
	}
	
	@Action("editcd_ProductClass")
	public String editcdProductClass(){
		return SUCCESS;
	}
	@Clew(results = { "success", "none" })
	@Action(value = "savecdProductClass", results = {
			@Result(name = "success", type = "redirect", location = "listcd_ProductClass"),
			@Result(name = "error", type = "chain", location = "editcd_ProductClass") ,
			@Result(name = "none", type = "redirect", location = "listcd_ProductClass")})
	public String savecdProductClass(){
		
		if(cdProductClass.getProductClassName()==null||cdProductClass.equals("")){
			this.addActionError("产品分类名称不能为空");
			return ERROR;
		}
		
		if(cdProductClass.getProductClassName()!=null){
		
			cd_ProductClass cpObject=this.getCd_ProductClassService().getcd_ProductClass(cdProductClass.getProductClassName());			
//		System.out.println("cpObject="+cpObject);
		if(cpObject!=null){
			this.addActionError("该产品分类名称已经存在了");
			return ERROR;
		}
		
		}
		this.cd_ProductClassService.save(cdProductClass);
		this.setPrompt("产品分类提交成功");
		return SUCCESS;
	}
	
	@Action("listcd_ProductClass")
	public String listCdProductClass(){
		
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				cd_ProductClass.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCd_ProductClassService().listByPage(criteria,pagination));
		return SUCCESS;
	}
	@Action(value="removeCdProductClass",results={@Result(name="success",type="redirect",location="listcd_ProductClass")})
	public String removeCdProductClass(){
		this.getCd_ProductClassService().batchRemove(ids);
		return SUCCESS;
	}
	
}
